package cn.com.nes.site.controller.app;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.iot.DtuService;
import cn.com.nes.site.service.app.AppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Controller
@RequestMapping("v2/app")
public class AppIotController {
    @Resource
    private DtuService dtuService;
    @Resource
    private AppService appService;
    //app扫码绑定设备//设备名称添加
    @RequestMapping("/addMyDev")
    @ResponseBody
    public PageData addMyDev(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = dtuService.addMyDev(req);
        return res;
    }
   //重发查询指令//设备健康检测
    @RequestMapping("/queryCommand")
    @ResponseBody
    public PageData queryCommand(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = null;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = appService.queryCommand(req);
        return res;
    }
    //查询设备列表，我得设备，设备详情，检测报告
    @RequestMapping("/getMyDevList")
    @ResponseBody
    public PageData getMyDevList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = null;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = appService.getMyDevList(req);
        return res;
    }
    //电池详情3,电池详情充放电时长、电池详情1
    @RequestMapping("/getBatteryDetails")
    @ResponseBody
    public PageData getBatteryDetails(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = null;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = appService.getHistoryData(req);
        return res;
    }
    //查询故障列表
    @RequestMapping("/getFaultRecords")
    @ResponseBody
    public PageData getFaultRecords( HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = null;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        return appService.getFaultRecords(req);
    }
}
